package com.friendfeed.api;

import java.text.ParseException;
import java.util.Date;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.Collection;
import java.io.InputStream;
import java.io.IOException;

public class FriendFeedUtils {
    private static final ThreadSafeSimpleDateFormat sdf = new ThreadSafeSimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z' zzz", 5, 20, false);

    public static Date parseDate(String date) throws ParseException {
        if (date == null || "".equals(date)) {
            return null;
        }

        try {
            Date d = sdf.parse(date + " UTC");
            return d;
        } catch (Exception e) {
            return null;
        }
    }

    public static String join(Collection c, String separator) {
        StringBuilder sb = new StringBuilder();
        for (Object o : c) {
            sb.append(o).append(separator);
        }
        sb.deleteCharAt(sb.length() - 1);

        return sb.toString();
    }

    public static void writeInputStream(InputStream ss, StringBuilder sb) throws IOException {
        byte[] buffer = new byte[2048];
        int length;
        while ((length = ss.read(buffer)) != -1) {
            sb.append(new String(buffer, 0, length));
        }
        ss.close();
    }
}
